﻿SET QUOTED_IDENTIFIER ON
GO
SET ARITHABORT ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO

-- =============================================
-- Author:		Adrian Kaczmarek
-- Create date: 2013-08-15
-- =============================================
CREATE PROCEDURE [dbo].[bhp_NemoGroupViewCreate] (
	@SystemName			NVARCHAR(400),
	@SelectDeclaration	NVARCHAR(MAX)
)
WITH ENCRYPTION
AS
BEGIN

	DECLARE 
		@Err			NVARCHAR(255)

	IF EXISTS (SELECT 1 FROM n_Tree WHERE name = @SystemName AND typ IN ('T', 'TE', 'V'))
	BEGIN
		SET @Err = 'Grupa o nazwie ' + @SystemName + ' już istnieje!';
		RAISERROR (@Err, 10, 1)
		RETURN;
	END

	SET @SelectDeclaration = 'CREATE VIEW [' + @SystemName + '] AS ' + @SelectDeclaration
	EXEC sp_executesql @SelectDeclaration

	EXEC [dbo].[n_ViewAdd] @SystemName, 900900
	SELECT id FROM n_Tree t WHERE t.name = @SystemName AND t.parent = 0

END
